# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=nix
pkgver=2.9.2
pkgrel=0
pkgdesc="The purely functional package manager"
url="https://nixos.org/nix/"
arch="x86 x86_64 aarch64"  # supported platforms
license="LGPL-2.1-or-later"
depends="ca-certificates"
depends_dev="
	boost-dev
	gc-dev
	"
makedepends="
	$depends_dev
	autoconf
	autoconf-archive
	automake
	bash
	bison
	brotli-dev
	bsd-compat-headers
	bzip2-dev
	curl-dev
	editline-dev
	flex
	gtest-dev
	jq
	libarchive-dev
	libseccomp-dev
	libsodium-dev
	libtool
	lowdown
	lowdown-dev
	mdbook
	nlohmann-json
	openssl-dev
	sqlite-dev
	xz
	xz-dev
	"
pkggroups="nix nixbld"
install="$pkgname.pre-install"
subpackages="
	$pkgname-dev
	$pkgname-openrc
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/NixOS/nix/archive/$pkgver/$pkgname-$pkgver.tar.gz
	fix-docs-build.patch
	nix-daemon.initd
	nix-profile.sh
	"

case "$CARCH" in
	x86*) makedepends="$makedepends libcpuid-dev";;
esac

prepare() {
	default_prepare

	./bootstrap.sh
}

build() {
	# NOTE: /nix/var is recommended by upstream and we need /nix anyway.
	bash ./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/nix/var \
		--enable-gc
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	cd "$pkgdir"

	# Remove trash.
	rm -R etc/init
	rm -R usr/lib/systemd
	rm etc/profile.d/*

	# Remove HTML docs.
	rm -rf usr/share/doc

	mkdir -p usr/sbin
	rm -f usr/bin/nix-daemon
	ln -s ../bin/nix usr/sbin/nix-daemon

	mv usr/share/fish/vendor_completions.d usr/share/fish/completions

	install -m 755 -D "$srcdir"/nix-profile.sh etc/profile.d/nix.sh
	install -m 755 -D "$srcdir"/nix-daemon.initd etc/init.d/nix-daemon

	# Setup build users.
	# Note: max-jobs should equal number of created nixbld<n> users.
	install -d -m 0555 etc/nix
	cat > etc/nix/nix.conf <<-EOF
		build-users-group = nixbld
		max-jobs = 4
	EOF

	# This is based on https://github.com/NixOS/nix/blob/9617a04/scripts/install-multi-user.sh#L525
	# NOTE: store dir must be /nix/store, otherwise pre-built binaries from
	#   the standard Nixpkgs channels would not work.
	install -d -m 1775 -g nixbld \
		nix/store
	install -d -m 0755 \
		nix/var/log/nix/drvs \
		nix/var/nix/db \
		nix/var/nix/gcroots \
		nix/var/nix/manifests \
		nix/var/nix/profiles/default \
		nix/var/nix/temproots \
		nix/var/nix/userpool \
		nix/var/nix/profile/per-user/root
	install -d -m 1777 \
		nix/var/nix/profiles/per-user \
		nix/var/nix/gcroots/per-user
	# Allow only users in the nix group to perform Nix operations.
	install -d -m 0770 -g nix \
		nix/var/nix/daemon-socket
}

sha512sums="
88ce3b9c06012b53923ef5b20260e0d7964979a38a393cf9915133f5ccb590dec46b1ce86d6c8f71a1f65872d3f2c47c96077240c8375bfdbee9916feab28b6a  nix-2.9.2.tar.gz
238267bf5680e7b68904e746c2443b20068aee33561ad7f4b2ddd6913ac1fce7703ed8f3d3170f3a8005a56aaa7d8b826fbd42af2855631204cc3cea191d0674  fix-docs-build.patch
9e4568db987fbae5097f7bb086e8ad51742c2d229703104fb7e6f240ffb0ca72e57dc305fb238bcee4ec8002d780007af60f241b1f32b6aa413dd579d9c59e75  nix-daemon.initd
0fb5b9ff2f593dca22251b342494ea0b61380d8f905d85855282d259c112aa2a918d9785aa08f847c693a939ebae9fbefcd4e417ad93283a142e066105cc87b5  nix-profile.sh
"
